package com.cssw.fyzb.core.dao.turntable;


import com.cssw.fyzb.core.dao.adnumprice.AdNumPriceDao;
import com.cssw.fyzb.core.model.dodb.AdNumPrice;
import com.cssw.fyzb.core.model.dodb.AdNumPriceLog;
import com.cssw.fyzb.core.model.dodb.TurntableNumPriceLog;
import com.cssw.fyzb.core.model.dodb.UserExt;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Repository
public class TurntableNumPriceLogDaoImpl implements TurntableNumPriceLogDao {

    @Override
    public TurntableNumPriceLog getTurnNumPriceLog(Long userId) {
        int dayTime = TimeUtil.getTimeYYMMDD(new Date());
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("dayTime", dayTime);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM t_turntable_num_price_log ");
        sqlParse.addSQL(" WHERE user_id=:userId AND day_time=:dayTime ");
        return TurntableNumPriceLog.dao.findFirst(sqlParse);
    }

    @Override
    public void updateLog(TurntableNumPriceLog log) {
        log.update();
    }

    @Override
    public Long saveLog(Long userId, Integer num, BigDecimal price) {
        int dayTime = TimeUtil.getTimeYYMMDD(new Date());
        TurntableNumPriceLog log = new TurntableNumPriceLog();
        log.setUserId(userId);
        log.setDayTime(dayTime);
        log.setNum(1);
        if (null != price) {
            log.setPrice(price);
        } else {
            log.setPrice(BigDecimal.ZERO);
        }
        log.setCreateTime(new Date());
        log.save();
        return log.getLogId();
    }
}
